package io.ent.modules.product.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.ent.common.utils.Query;
import io.ent.common.validator.ValidatorUtils;
import io.ent.modules.product.entity.ProductCasePicsEntity;
import io.ent.modules.product.po.ProductCaseInfoPicEntityIfPo;
import io.ent.modules.product.po.ProductInfoEntityIfPo;
import io.ent.modules.product.po.ProductInfoEntityPo;
import io.ent.modules.product.po.ProductPicPo;
import io.ent.modules.product.service.ProductCasePicsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.ent.modules.product.entity.ProductInfoEntity;
import io.ent.modules.product.service.ProductInfoService;
import io.ent.modules.sys.shiro.ShiroUtils;
import io.ent.common.utils.PageUtils;
import io.ent.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 15:50:16
 */
@RestController
@RequestMapping("product/productinfo")
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductCasePicsService productCasePicsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:productinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:productinfo:info")
    public R info(@PathVariable("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        ProductInfoEntity productInfo = productInfoService.getById(id);
        List<ProductCasePicsEntity> pics = productCasePicsService.getAllPics(id,1);
        map.put("productInfo", productInfo);
        map.put("pics", pics);
        return R.ok().put("res", map);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:productinfo:save")
    public R save(@RequestBody ProductInfoEntityPo productInfo){
        String username = ShiroUtils.getUserEntity().getUsername();
    	productInfo.setCreater(username);
    	productInfo.setCreateTime(new Date());
    	productInfo.setUpdater(username);
    	productInfo.setUpdateTime(new Date());
        productInfoService.save(productInfo);
        List<ProductPicPo> picList = productInfo.getPicList();
        for (int i = 0; i < picList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productInfo.getId());
            picsEntity.setPicName(picList.get(i).getPicName());
            picsEntity.setPicAddress(picList.get(i).getPicAddress());
            picsEntity.setPicType(1);
            picsEntity.setCreater(username);
            picsEntity.setCreateTime(new Date());
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:productinfo:update")
    public R update(@RequestBody ProductInfoEntityPo productInfo){
        ValidatorUtils.validateEntity(productInfo);
        String username = ShiroUtils.getUserEntity().getUsername();
        productInfoService.updateById(productInfo);
        List<ProductPicPo> picList = productInfo.getPicList();
        for (int i = 0; i < picList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productInfo.getId());
            picsEntity.setPicName(picList.get(i).getPicName());
            picsEntity.setPicAddress(picList.get(i).getPicAddress());
            picsEntity.setPicType(1);
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:productinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        productInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //查找所有产品信息---公开的接口
    @RequestMapping("/getProductInfos")
    public R getProductInfos(@RequestParam Map<String, Object> params){
        PageUtils productInfos = productInfoService.getProductInfos(params);
        return R.ok().put("productInfos",productInfos);
    }

    //查找所有产品信息(已分好类)---公开的接口
    @RequestMapping("/getProductTypeInfos")
    public R getProductTypeInfos(@RequestParam Map<String, Object> params){
        IPage<ProductInfoEntityIfPo> page = new Query<ProductInfoEntityIfPo>().getPage(params);
        Map<String, Object> map = new HashMap<>();
        //查询新品产品信息
        PageUtils xp = productInfoService.getProductInfosByTypeId(1, page.getCurrent(),page.getSize());

        //查询热销产品信息
        PageUtils rx = productInfoService.getProductInfosByTypeId(2, page.getCurrent(),page.getSize());

        //查询铝扣板产品信息
        PageUtils lkb = productInfoService.getProductInfosByTypeId(3, page.getCurrent(),page.getSize());

        //查询吕梁产品信息
        PageUtils ll = productInfoService.getProductInfosByTypeId(4, page.getCurrent(),page.getSize());

        //查询圆顶产品信息
        PageUtils yd = productInfoService.getProductInfosByTypeId(5, page.getCurrent(),page.getSize());

        //查询电器产品信息
        PageUtils dq = productInfoService.getProductInfosByTypeId(6, page.getCurrent(),page.getSize());

        map.put("xp",xp);
        map.put("rx",rx);
        map.put("lkb",lkb);
        map.put("ll",ll);
        map.put("yd",yd);
        map.put("dq",dq);
        return R.ok().put("productInfo",map);
    }

}
